package com.always.electrical.energy.web.service;

import com.always.electrical.energy.web.data.entity.CjMpptGlqx;
import com.always.electrical.energy.web.data.entity.EfbsMaintMeasurement;
import com.always.electrical.energy.web.data.entity.IotSocReportData;
import com.always.electrical.energy.web.res.MscChargeRes;
import com.always.electrical.energy.web.res.MscMpptRes;
import com.always.electrical.energy.web.res.MscStorageRes;
import com.always.electrical.energy.web.res.MscWindPowerRes;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 光、储、充
 */
@Service
public class MscService extends BaseService {

    // 储能中心（储能）
    public MscStorageRes storage(List<BigDecimal> deviceIds) {
        MscStorageRes webRes = new MscStorageRes();
        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, IotSocReportData> iotSocReportDataMap = iotSocReportDataService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(IotSocReportData::getSocId, e -> e));

            Map<BigDecimal, String> efbsMaintMeasurementMap = efbsMaintMeasurementService.lambdaQuery()
                    .in(EfbsMaintMeasurement::getId, deviceIds)
                    .list()
                    .stream()
                    .filter(item -> item.getExtendfield4() != null)
                    .collect(Collectors.toMap(EfbsMaintMeasurement::getId, EfbsMaintMeasurement::getExtendfield4));

            for (BigDecimal deviceId : deviceIds) {
                IotSocReportData iotSocReportData = iotSocReportDataMap.getOrDefault(deviceId, new IotSocReportData());
                webRes.getItems().add(new MscStorageRes.Item().setId(deviceId)
                        .setState(iotSocReportData.getRunst())
                        .setElect(iotSocReportData.getSoc())
                        .setCapacity(efbsMaintMeasurementMap.get(deviceId))
                        .setW(iotSocReportData.getZlgl())
                        .setV(iotSocReportData.getZldy())
                        .setA(iotSocReportData.getZldl())
                );
            }
        }
        return webRes;
    }

    // 风机
    public MscWindPowerRes windPower(List<BigDecimal> deviceIds) {
        MscWindPowerRes webRes = new MscWindPowerRes();
        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, IotSocReportData> iotSocReportDataMap = iotSocReportDataService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(IotSocReportData::getSocId, e -> e));

            for (BigDecimal deviceId : deviceIds) {
                IotSocReportData iotSocReportData = iotSocReportDataMap.getOrDefault(deviceId, new IotSocReportData());
                webRes.getItems().add(new MscWindPowerRes.Item().setId(deviceId)
                        .setRunst(iotSocReportData.getRunst())
                        .setWtVInv(iotSocReportData.getWtVInv())
                        .setWtAInv(iotSocReportData.getWtAInv())
                        .setWtTotwh(iotSocReportData.getWtTotwh())
                        .setWtWInv(iotSocReportData.getWtWInv())
                );
            }
        }
        return webRes;
    }

    // 充电中心（充电桩）
    public MscChargeRes charge(List<BigDecimal> deviceIds) {
        MscChargeRes webRes = new MscChargeRes();
        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, CjMpptGlqx> cjMpptGlqxMap = cjMpptGlqxService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(CjMpptGlqx::getDeviceId, e -> e));

            for (BigDecimal deviceId : deviceIds) {
                CjMpptGlqx cjMpptGlqx = cjMpptGlqxMap.getOrDefault(deviceId, new CjMpptGlqx());
                webRes.getItems().add(new MscChargeRes.Item().setId(deviceId)
                        .setRunstMd1(cjMpptGlqx.getRunstMd1())
                        .setRunstMd2(cjMpptGlqx.getRunstMd2())
                        .setWMd1(cjMpptGlqx.getWMd1())
                        .setWMd2(cjMpptGlqx.getWMd2())
                        .setChargeenableMd1(cjMpptGlqx.getChargeenableMd1())
                        .setChargeenableMd2(cjMpptGlqx.getChargeenableMd2()));
            }
        }
        return webRes;
    }

    // 光伏（MPPT)
    public MscMpptRes mppt(List<BigDecimal> deviceIds) {
        MscMpptRes webRes = new MscMpptRes();

        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, CjMpptGlqx> cjMpptGlqxMap = cjMpptGlqxService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(CjMpptGlqx::getDeviceId, e -> e));

            for (BigDecimal deviceId : deviceIds) {

                CjMpptGlqx cjMpptGlqx = cjMpptGlqxMap.getOrDefault(deviceId, new CjMpptGlqx());
                webRes.getItems().add(new MscMpptRes.Item().setId(deviceId)
                                .setRunst(cjMpptGlqx.getRunst())
                                .setZgl(Stream.of(cjMpptGlqx.getDylgl(), cjMpptGlqx.getDelgl(), cjMpptGlqx.getDelgl())
                                        .filter(Objects::nonNull)
                                        .reduce(BigDecimal.ZERO, BigDecimal::add))
                        );
            }
        }

        return webRes;
    }
}
